#ifndef TYPES_H
#define TYPES_H

// 所有类型定义和宏
typedef unsigned char      u8;
typedef unsigned short     u16;
typedef unsigned int       u32;
typedef unsigned long long u64;

typedef signed char      s8;
typedef signed short     s16;
typedef signed int       s32;
typedef signed long long s64;

#ifdef __cplusplus
#include <stdbool.h>
#else
#ifndef bool
typedef enum
{
    FALSE = 0,
    TRUE = !FALSE
} bool;
#endif
#endif

// 大小端转换宏
#define SWAP16( x ) ( ( ( (x)&0xff00 ) >> 8 ) | ( ( (x)&0x00ff ) << 8 ) )
#define SWAP32( x ) ( ( ( (x)&0xff000000 ) >> 24 ) | ( ( (x)&0x00ff0000 ) >> 8 ) | \
                      ( ( (x)&0x0000ff00 ) << 8 ) | ( ( (x)&0x000000ff ) << 24 ) )

// 位操作宏
#define BIT( x )        ( 1UL << ( x ) )
#define SET_BIT( x, b ) ( ( x ) |= BIT( b ) )
#define CLR_BIT( x, b ) ( ( x ) &= ~BIT( b ) )
#define TGL_BIT( x, b ) ( ( x ) ^= BIT( b ) )
#define GET_BIT( x, b ) ( ( (x)&BIT( b ) ) != 0 )

// 对齐宏
#define ALIGN( x, a ) ( ( ( x ) + ( (a)-1 ) ) & ~( (a)-1 ) )


// 内联函数和弱符号宏
#define INLINE __inline__
#define WEAK   __attribute__( ( weak ) )


#if defined(__arm__) || defined( __ARMCC_VERSION )
#define CTD_SECTION( x ) __attribute__( ( section( x ) ) )
#define CTD_USED __attribute__( ( used ) )
#define CTD_ALIGN( n ) __attribute__( ( aligned( n ) ) )
#define CTD_WEAK __attribute__( ( weak ) )
#define CTD_PACK __attribute__( ( packed ) )
#elif defined(__GNUC__)
#define CTD_SECTION( x ) __attribute__( ( __section__( x ) ) )
#define CTD_USED __attribute__( ( __used__ ) )
#define CTD_ALIGN( n ) __attribute__( ( aligned( n ) ) )
#define CTD_WEAK __attribute__( ( weak ) )
#define CTD_PACK __attribute__( ( packed ) )
#else
#error "Unknown compiler!"
#endif


#endif // TYPES_H
